﻿// var authority = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]; //用户权限列表
//0: 查看项目 1: 管理项目 2: 查看委托单和委托人 3: 查看、新建（自己的）委托单 4: 新建、查看委托单和委托人
//5: 查看报告 6: 发布报告 7: 制定报告 8: 审核报告 9: 查看通知单 10: 发布通知单（评片） 11: 新建、匹配工艺卡
//12: 审核工艺卡 13: 分配检测小组 14: 分配检测人员 15: 查看派工单 16: 管理检测小组 17: 填写操作记录
//18: 分配权限 19: 检测部门管理
if (authority[16] === 0) {
    alert("你没有权限查看此内容");
    window.location.href = "index.html";
}
var rawData = [[], [], []];//数据，第一项为编号
var sentMessage;
if (authority[18] === 1){
    sentMessage = {
        'requestType':'superManager'
    }
} else if(authority[16] === 1){
    sentMessage = {
        "requestType": "list",
        "part_id": user.staff_part
    }
}
$.ajax({
    url: '/user/team',
    type: 'post',
    data: sentMessage,
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            console.log(data);
            for (let i = 0; i < data.length; i++) {
                rawData[0].push([
                    data[i].team_id,
                    data[i].team_name,
                    data[i].user_name
                ]);
            }
        }
    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});
var dummyData = [].concat(rawData);
var title;
var methodCount;
//初始化表格标题，根据权限类型动态展示新建按钮和选项卡
if (authority[16] === 1) {
    title = [["编号", "检测小组", "组长", "详细信息"]];
    document.getElementById("new").innerHTML = "<button onclick=\"window.location.href='/newGroup.html'\" class=\"btn btn-xs btn-success\"><i class=\"ace-icon fa fa-plus bigger-120\"> 新建小组</i></button>";
    methodCount = [3, 3, 3];
}

var pageID = [1, 1, 1];
//设置每一页的行数
var countPerPage=[];

//默认情况下
if(user.countPerPage== 0){
    countPerPage = [10, 10, 10];
    $('.rowNum').val(10)
}else {
    countPerPage = [user.countPerPage,user.countPerPage,user.countPerPage];
    $('.rowNum').val(user.countPerPage)
}

//点击设置每一页行数的确定按钮
$('.rowsBut').click(function () {
    //获得文本框中输入的值
    var rowsNumbuer = $('.rowNum').val();
    //将该值传入到后台中
    $.ajax({
        url:'/pj/countPerPage',
        type:'post',
        data:{'rowsNumbuer': rowsNumbuer},
        async:false,
        success:function () {
            //console.log('点击确认以后的countPerPage'+user.userId)
            if(user.countPerPage== 0){
                countPerPage = [10, 10, 10];
                $('.rowNum').val(10)
            }else {
                countPerPage = [user.countPerPage,user.countPerPage,user.countPerPage];
                $('.rowNum').val(user.countPerPage)
            }
        },
        error:function () {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    //刷新页面
    location.reload()
})
var pageButtonCount = [8, 8, 8];
var pageCount = [parseInt((dummyData[0].length - 1) / countPerPage[0]) + 1,
parseInt((dummyData[1].length - 1) / countPerPage[1]) + 1,
parseInt((dummyData[2].length - 1) / countPerPage[2]) + 1];
var table = [document.getElementById("table0")];
var tbody = [table[0].children[1]];
var pages = [document.getElementById("pages0")];
console.log(tbody[0].innerHTML);
//初始化表格
for (let i = 0; i < 1; i++) {
    let inner = "<td>&nbsp;</td>";
    for (let j = 0; j < title[i].length - 1; j++) {
        inner += "<td />";
    }
    for (let j = 0; j < countPerPage[i]; j++) {
        var tr = document.createElement("tr");
        tr.innerHTML = inner;
        tbody[i].appendChild(tr);
    }
}
//装填标题栏
for (let i = 0; i < 1; i++) {
    for (let j = 0; j < (pageCount[i] > pageButtonCount[i] ? pageButtonCount[i] : pageCount[i]); j++) {
        var li = document.createElement("li");
        li.setAttribute("onclick", "OnClick(" + (j + 1) + ", " + i + ")");
        var a = document.createElement("a");
        a.innerHTML = j + 1;
        a.setAttribute("href", "#");
        li.appendChild(a);
        pages[i].insertBefore(li, pages[i].lastElementChild);
    }
    for (let j = 0; j < title[i].length; j++) {
        let t = document.createElement("th");
        t.innerHTML = title[i][j];
        table[i].children[0].children[0].appendChild(t);
    }
    OnClick(1, i);
}
// 生成翻页按钮
// console.log(pages[0].innerHTML);
function OnClick(index, cardIndex) {
    // console.log(cardIndex);
    // console.log("a" + pageID);
    if (index === -1) {
        if (pageID[cardIndex] > 1) OnClick(pageID[cardIndex] - 1, cardIndex);
        return;
    }
    if (index === -2) {
        // console.log("b" + pageID);
        if (pageID[cardIndex] < pageCount[cardIndex]) OnClick(pageID[cardIndex] + 1, cardIndex);
        return;
    }
    if (index === 1 && dummyData[cardIndex].length < countPerPage[cardIndex]) {
        for (let i = dummyData[cardIndex].length; i < countPerPage[cardIndex]; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "display: none");
        }
        pages[cardIndex].setAttribute("style", "display: none");
    }
    else {
        for (let i = 0; i < countPerPage[cardIndex]; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "");
        }
        pages[cardIndex].setAttribute("style", "");
    }
    pageID[cardIndex] = index;
    // console.log("c" + pageID);
    //显示数据
    for (let i = 0; i < countPerPage[cardIndex]; i++) {
        if (dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i] !== undefined) {
            for (let j = 0; j < methodCount[cardIndex]; j++) {
                tbody[cardIndex].children[i].children[j].innerHTML = dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][j];
            }
            //显示操作按钮
            if (authority[16] === 1) {
                let button = document.createElement("button");
                button.setAttribute("class", "btn btn-xs btn-success");
                button.setAttribute("onclick", "window.location.href='/viewGroup.html?id="
                    + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");//pao
                button.innerHTML = "修改";
                tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML;
            }
        } else {
            tbody[cardIndex].children[i].children[0].innerHTML = "&nbsp;";
            for (let j = 1; j < title[cardIndex].length; j++) {
                tbody[cardIndex].children[i].children[j].innerHTML = "";
            }
        }
    }
    //刷新翻页按钮
    if (pageCount[cardIndex] > pageButtonCount[cardIndex]) {
        var first = parseInt(pageID[cardIndex] - pageButtonCount[cardIndex] / 2) + 1;
        if (first < 1) first = 1;
        if (first > pageCount[cardIndex] - pageButtonCount[cardIndex] + 1) first = pageCount[cardIndex] - pageButtonCount[cardIndex] + 1;
        for (let i = 0; i < pageButtonCount[cardIndex]; i++) {
            pages[cardIndex].removeChild(pages[cardIndex].children[1]);
        }
        for (let i = first; i < first + pageButtonCount[cardIndex]; i++) {
            var li = document.createElement("li");
            li.setAttribute("onclick", "OnClick(" + i + ", " + cardIndex + ")");
            if (i === pageID[cardIndex])
                li.setAttribute("class", "active");
            var a = document.createElement("a");
            a.innerHTML = i;
            a.setAttribute("href", "#");
            li.appendChild(a);
            pages[cardIndex].insertBefore(li, pages[cardIndex].lastElementChild);
        }
    } else {
        for (let i = 0; i < pageCount[cardIndex]; i++) {
            pages[cardIndex].removeChild(pages[cardIndex].children[1]);
        }
        for (let i = 0; i < pageCount[cardIndex]; i++) {
            li = document.createElement("li");
            li.setAttribute("onclick", "OnClick(" + (i + 1) + ", " + cardIndex + ")");
            if (i + 1 === pageID[cardIndex])
                li.setAttribute("class", "active");
            a = document.createElement("a");
            a.innerHTML = i + 1;
            a.setAttribute("href", "#");
            li.appendChild(a);
            pages[cardIndex].insertBefore(li, pages[cardIndex].lastElementChild);
        }
    }
    // console.log("d" + pageID);
}

function OnConfirmDepute() {
    window.location.href = "newDepute.html?id=" + projectName[projectOption.selectedIndex][1];
}